/* Copyright (c) 2004-2008, Andriy Nikolov
 * All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.processmining.email;

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.text.*;
import java.util.*;
import com.borland.jbcl.layout.BoxLayout2;
import com.borland.jbcl.layout.VerticalFlowLayout;
import javax.swing.border.TitledBorder;

/**
 * <p>Represents the e-mail log interface frame in the standalone version
 * @author Andriy Nikolov
 * @version 1.0
 */

public class EMailLogFrame extends JFrame {
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel jPanelEMailLog = new JPanel();
  JScrollPane jScrollPaneEMailLog = new JScrollPane();
  BorderLayout borderLayout2 = new BorderLayout();
  EMailLog eMailLog;
  EMailLogTableModel eMailLogTableModel;
  EmployeesListTableModel employeesListTableModel;
  JTabbedPane jTabbedPaneEMailLog = new JTabbedPane();
  JTable jTableEMailLog = new JTable();
  JPanel jPanelEmployeesFilter = new JPanel();
  JPanel jPanelEmployeesList = new JPanel();
  JPanel jPanelButtons = new JPanel();
  VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
  JPanel jPanelAliases = new JPanel();
  BorderLayout borderLayout3 = new BorderLayout();
  BorderLayout borderLayout4 = new BorderLayout();
  JScrollPane jScrollPaneEmployeesList = new JScrollPane();
  JTable jTableEmployees = new JTable();
  ListSelectionModel employeesListSelectionModel;
  JScrollPane jScrollPaneAliasesList = new JScrollPane();
  JList jListAliases = new JList();
  JPanel jPanelEmployeesListHeader = new JPanel();
  JPanel jPanelEmployeesListFooter = new JPanel();
  JLabel jLabelUsers = new JLabel();
  JPanel jPanelAliasesHeader = new JPanel();
  JPanel jPanelAliasesFooter = new JPanel();
  TitledBorder titledBorder1 = new TitledBorder("");
  JLabel jLabelAliases = new JLabel();
  JButton jButtonMerge = new JButton();
  JButton jButtonMainName = new JButton();
  int currentEmployee;
  DefaultListModel aliasesListModel = new DefaultListModel();
  GridLayout gridLayout1 = new GridLayout();
  BorderLayout borderLayout5 = new BorderLayout();
  JTextField jTextFieldNewAlias = new JTextField();
  JButton jButtonAddAlias = new JButton();

  public EMailLogFrame() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  public EMailLogFrame(EMailLog log) {
    try {
      eMailLog = log;
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.setSize(700, 500);
    this.setTitle("E-mail Log");
    this.getContentPane().setLayout(borderLayout1);
    jPanelEMailLog.setLayout(borderLayout2);
    jListAliases.setBorder(BorderFactory.createEtchedBorder());
    jListAliases.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    eMailLogTableModel = new EMailLogTableModel(eMailLog);
    jTableEMailLog.setModel(eMailLogTableModel);
    jTableEMailLog.getColumnModel().getColumn(0).setMaxWidth(25);
    jTableEMailLog.getColumnModel().getColumn(4).setMaxWidth(70);
    jTableEMailLog.getColumnModel().getColumn(5).setMaxWidth(50);

    employeesListTableModel = new EmployeesListTableModel(eMailLog);
    jTableEmployees.setBorder(null);
    jTableEmployees.setModel(employeesListTableModel);
    jTableEmployees.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    employeesListSelectionModel = jTableEmployees.getSelectionModel();
    employeesListSelectionModel.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        jTableEmployees_selectionValueChanged(e);
      }
    } );

    jTableEmployees.getColumnModel().getColumn(1).setMaxWidth(50);
    jListAliases.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jListAliases.setModel(aliasesListModel);
    jPanelEmployeesFilter.setLayout(gridLayout1);
    jPanelEmployeesList.setLayout(borderLayout3);
    jPanelButtons.setLayout(verticalFlowLayout1);
    jPanelAliases.setLayout(borderLayout4);
    jLabelUsers.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jLabelUsers.setText("Users");
    jPanelEmployeesListHeader.setBorder(null);
    jLabelAliases.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jLabelAliases.setText("Aliases");
    jButtonMerge.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jButtonMerge.setToolTipText("Merge several employees into one");
    jButtonMerge.setText("Merge");
    jButtonMerge.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonMerge_actionPerformed(e);
      }
    });
    jPanelEmployeesList.setBorder(BorderFactory.createEtchedBorder());
    jPanelAliases.setBorder(BorderFactory.createEtchedBorder());
    jButtonMainName.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jButtonMainName.setToolTipText("Set main name for an employee");
    jButtonMainName.setText("Main name");
    jButtonMainName.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonMainName_actionPerformed(e);
      }
    });
    jTabbedPaneEMailLog.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jPanelAliasesFooter.setLayout(borderLayout5);
    jTextFieldNewAlias.setText("");
    jButtonAddAlias.setText("Add alias");
    jButtonAddAlias.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jButtonAddAlias.setToolTipText("Add new alias name");
    jButtonAddAlias.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonAddAlias_actionPerformed(e);
      }
    });

    jTabbedPaneEMailLog.add(jPanelEMailLog, "Message List");
    jPanelEMailLog.add(jScrollPaneEMailLog, BorderLayout.CENTER);
    jTabbedPaneEMailLog.add(jPanelEmployeesFilter, "Employees List");
    jPanelEmployeesList.add(jScrollPaneEmployeesList, java.awt.BorderLayout.CENTER);
    jScrollPaneEmployeesList.getViewport().add(jTableEmployees);
    jScrollPaneEMailLog.getViewport().add(jTableEMailLog);
    this.getContentPane().add(jTabbedPaneEMailLog, java.awt.BorderLayout.CENTER);
    jPanelAliases.add(jScrollPaneAliasesList, java.awt.BorderLayout.CENTER);
    jScrollPaneAliasesList.getViewport().add(jListAliases);
    jPanelEmployeesList.add(jPanelEmployeesListHeader,
                            java.awt.BorderLayout.NORTH);
    jPanelEmployeesListHeader.add(jLabelUsers);
    jPanelEmployeesList.add(jPanelEmployeesListFooter,
                            java.awt.BorderLayout.SOUTH);
    jPanelEmployeesListFooter.add(jButtonMerge);
//    jPanelEmployeesFilter.add(jPanelButtons);
    jPanelAliases.add(jPanelAliasesFooter, java.awt.BorderLayout.SOUTH);
    jPanelAliases.add(jPanelAliasesHeader, java.awt.BorderLayout.NORTH);
    jPanelAliasesHeader.add(jLabelAliases);
    jPanelEmployeesFilter.add(jPanelEmployeesList, null);
    jPanelEmployeesFilter.add(jPanelAliases, null);
    jPanelAliasesFooter.add(jButtonMainName, java.awt.BorderLayout.WEST);
    jPanelAliasesFooter.add(jButtonAddAlias, java.awt.BorderLayout.EAST);
    jPanelAliasesFooter.add(jTextFieldNewAlias, java.awt.BorderLayout.NORTH);
    currentEmployee = -1;
  }

  public void jTableEmployees_selectionValueChanged(ListSelectionEvent e) {
    ListSelectionModel lsm;
    lsm = (ListSelectionModel)e.getSource();
    if(!lsm.isSelectionEmpty()) {
      currentEmployee = lsm.getMinSelectionIndex();
      updateAliasesList();
    }
  }

  public void jButtonMerge_actionPerformed(ActionEvent e) {
    ListSelectionModel lsm;
    int minIndex, curIndex;
    int i;
    Vector selection = new Vector();
    EMailRecipient leader, tmp;
    lsm = (ListSelectionModel)jTableEmployees.getSelectionModel();
    if(!lsm.isSelectionEmpty()) {
      minIndex = lsm.getMinSelectionIndex();
      leader = (EMailRecipient)eMailLog.getEmployeesList().get(minIndex);
      for(i=0;i<jTableEmployees.getRowCount();i++) {
        if (lsm.isSelectedIndex(i)) {
          if(i!=minIndex) {
            tmp = (EMailRecipient) eMailLog.getEmployeesList().get(i);
            selection.add(tmp);
          }
        }
      }
      for(i=0;i<selection.size();i++) {
        tmp = (EMailRecipient)selection.get(i);
        eMailLog.mergeRecipients(leader, tmp);
      }
      jTableEmployees.revalidate();
      updateAliasesList();
    }
  }

  public void jButtonMainName_actionPerformed(ActionEvent e) {
    String name;
    EMailRecipient tmp;
    name = (String)aliasesListModel.get(jListAliases.getSelectedIndex());
    tmp = (EMailRecipient)eMailLog.getEmployeesList().get(currentEmployee);
    tmp.setName(name);
    jTableEmployees.revalidate();
  }


  public void updateAliasesList() {
    EMailRecipient eMailRecipient;
    String curAlias;
    aliasesListModel.clear();
    eMailRecipient = (EMailRecipient)eMailLog.getEmployeesList().get(currentEmployee);
    for (int i=0;i<eMailRecipient.getAliases().size();i++) {
      aliasesListModel.addElement(eMailRecipient.getAlias(i));
    }
  }

  public void setEMailLog(EMailLog log) {
    eMailLog = log;
  }

  public EMailLog getEMailLog() {
    return eMailLog;
  }

  public void jButtonAddAlias_actionPerformed(ActionEvent e) {
    String name;
    EMailRecipient tmp;

    name = jTextFieldNewAlias.getText();
    tmp = (EMailRecipient)eMailLog.getEmployeesList().get(currentEmployee);
    if(name!="")
      tmp.addAlias(name);
    jTextFieldNewAlias.setText("");
    updateAliasesList();
  }
}
